package com.xasnow;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@RestController
@SpringBootApplication
public class DemoApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    
    @GetMapping("/cpuload/{limit}")
    public String cpuLoad(@PathVariable("limit") int limit) {
//        method1();
        cpuLoad2(limit);
//        cpuConsume();
        return "";
    
    }
    
    //cpu消耗方法
    public void cpuConsume(int limit){
        // 角度的分割
        final double SPLIT = 0.01;
        //
        // 2PI分割的次数，也就是2/0.01个，正好是一周
        final int COUNT = (int) (2 / SPLIT);
        final double PI = Math.PI;
        // 时间间隔
        final int INTERVAL = 200;
        long[] busySpan = new long[COUNT];
        long[] idleSpan = new long[COUNT];
        int half = INTERVAL / 2;
        double radian = 0.0;
        for (int i = 0; i < COUNT; i++) {
            busySpan[i] = (long) (half + (Math.sin(PI * radian) * half));
            idleSpan[i] = INTERVAL - busySpan[i];
            radian += SPLIT;
        }
        long startTime = 0;
        int j = 0;
        while (true) {
            j = j % COUNT;
            startTime = System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < busySpan[j])
                ;
            try {
                
                //这里的if控制可以注解掉，让Thread.sleep(idleSpan[j])一直执行。
                //我这里加了if控制是因为希望Cpu一直保存在70%以上工作的效果(小于70不sleep)，If注解掉将以正弦曲线的趋势使用Cpu
                if(idleSpan[j]< 100-limit){
                    Thread.sleep(idleSpan[j]);
                }
            
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            j++;
        }
    }

    
    public String cpuLoad2(int limit) {
        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        
        for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
            final int finalLimit = limit;
            executorService.submit(() -> cpuConsume(finalLimit));
        }
        
        executorService.shutdown();
        try {
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        return "CPU Load Test Complete";
    }
    
    private String method1() {
        int result = 0;
        for (int i = 0; i < 100000000; i++) {
            result += i;
            System.out.println(result);
        }
//        while (true) {
//            // 执行一些计算密集型的操作
//
//        }
        return String.valueOf(result);
    }
}
